package io.dy.supplyAsync;

import io.dy.utils.CommonUtils;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author dmj
 * @date 2024年03月01日 下午8:45
 * 带返回值的异步任务
 */
public class SupplyAsync {

    public static void main(String[] args) throws ExecutionException, InterruptedException {

        ExecutorService executor = Executors.newFixedThreadPool(2);

        //需求: 异步线程读取dy.txt文件 并在主线程中打印文件内容
        CommonUtils.printThreadLog("main start");
        CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> CommonUtils.readFile("dy.txt"),executor);
        CommonUtils.printThreadLog("here not blocked main continue");
        CommonUtils.printThreadLog(future.get());
        CommonUtils.printThreadLog("main end");
    }
}
